import React, { Component } from 'react'
import moment from 'moment';


export default class LifeClock extends Component {
  //状态
  state = {
    timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
  }

  timer = null;

  render() {
    return (
      <div style={{
        border: 'solid 1px #520',
        display: 'inline-block',
        padding: '20px 40px'
      }}>
        {this.state.timeStr}
      </div>
    )
  }

  //生命周期钩子  只会执行一次
  componentDidMount(){
    //启动定时器  
    this.timer = setInterval(() => {
      console.log('1234');     
      //更新状态 
      this.setState({
        timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
      })
    }, 1000)
  }

  //组件将要卸载生命周期回调
  componentWillUnmount(){
    //停止定时器
    clearInterval(this.timer)
  }
}

